﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using Param = ClassLibrary.Parameters.Values;
using ClassLibrary;

namespace ControlLibrary
{
    public partial class Panel_EventEdition : UserControl
    {
        private bool newEvent = true;
        private DateTime dateStart
        {
            get { return dateTimePicker_Date1.Value; }
        }
        private DateTime dateEnd
        {
            get
            {
                if (radioButton_date.Checked)
                {
                    DateTime res = new DateTime(1, 1,1);
                    return res;
                }
                else return dateTimePicker_Date2.Value;
            }
        }
        private Guid _ID;
        private InitialState initialState;
        private bool _init;

        private struct InitialState
        {
            public DateTime d1, d2;
            public bool periode;
            public string titre, lien;
        }

        public delegate void OnModified();
        [Category("Configuration"), Browsable(true), Description("")]
        public event OnModified onModified;

        public delegate void OnSaved(Guid ID, string titre, string lien, DateTime d1, DateTime d2, bool periode, bool newEvent);
        [Category("Configuration"), Browsable(true), Description("")]
        public event OnSaved onSaved;

        #region CONSTRUCTOR
        public Panel_EventEdition()
        {
            InitializeComponent();   
        }
        #endregion

        #region private

        private void radioButton_date_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (radioButton_date.Checked && !radioButton_period.Checked)
                {
                    label_From.Text = Param.RM.GetString("PE_LabelDate");
                    label_To.Visible = false;
                    dateTimePicker_Date2.Visible = false;
                }
                else if (!radioButton_date.Checked && radioButton_period.Checked)
                {
                    label_From.Text = Param.RM.GetString("PE_LabelPeriod");
                    label_To.Visible = true;
                    dateTimePicker_Date2.Visible = true;
                }

                if (null != onModified && !_init)
                    onModified();
            }
            catch (Exception ex)
            {
                ErrorManager.WriteLog("PanelEvent", "radioButton_date_CheckedChanged", ex);
            }
        }

        //If start date change, check if end date is >
        private void dateTimePicker_Date1_CloseUp(object sender, EventArgs e)
        {
            if (dateTimePicker_Date2.Value.Year > dateTimePicker_Date1.Value.Year)
                dateTimePicker_Date2.Value = dateTimePicker_Date1.Value;
            if (dateTimePicker_Date2.Value < dateTimePicker_Date1.Value)
                dateTimePicker_Date2.Value = dateTimePicker_Date1.Value;

            if (null != onModified && !_init)
                onModified();
        }

        //If end date change, check if start date is <
        private void dateTimePicker_Date2_CloseUp(object sender, EventArgs e)
        {
            if (dateTimePicker_Date2.Value < dateTimePicker_Date1.Value)
                dateTimePicker_Date1.Value = dateTimePicker_Date2.Value;

            if (null != onModified && !_init)
                onModified();
        }

        private void textBox_Title_TextChanged(object sender, EventArgs e)
        {
            label_Error.Visible = false;

            if (null != onModified && !_init)
                onModified();
        }

        private void textBox_Link_TextChanged(object sender, EventArgs e)
        {
            if (null != onModified && !_init)
                onModified();
        }

        public void trySave()
        {
            if (null != onSaved)
            {
                //Check
                if (string.Empty == textBox_Title.Text)
                {
                    //label_Error.Text = Param.RM.GetString("PE_EmptyTitle");
                    label_Error.Visible = true;
                    textBox_Title.Focus();
                }
                else
                {

                    onSaved(_ID, textBox_Title.Text,
                        textBox_Link.Text,
                        dateStart,
                        dateEnd,
                        radioButton_period.Checked,
                        newEvent);
                    newEvent = false;

                    //Updtate initial state
                    initialState.d1 = dateStart;
                    initialState.d2 = dateEnd;
                    initialState.periode = radioButton_period.Checked;
                    initialState.titre = textBox_Title.Text;
                    initialState.lien = textBox_Link.Text;
                    
                }
            }
        }

        #endregion

        public void UpdateUI()
        {
            ResourceManager _RM = new ResourceManager("ControlLibrary.Panel_EventEdition", typeof(Panel_EventEdition).Assembly);
            try
            {
                if (radioButton_date.Checked && !radioButton_period.Checked)
                {
                    label_From.Text = Param.RM.GetString("PE_LabelDate");
                    label_To.Visible = false;
                    dateTimePicker_Date2.Visible = false;
                }
                else if (!radioButton_date.Checked && radioButton_period.Checked)
                {
                    label_From.Text = Param.RM.GetString("PE_LabelPeriod");
                    label_To.Visible = true;
                    dateTimePicker_Date2.Visible = true;
                }

                label_To.Text = _RM.GetString("label_To.Text");
                radioButton_period.Text = _RM.GetString("radioButton_period.Text");
                textBox_Link.CueText = _RM.GetString("textBox_Link.CueText");
                textBox_Title.CueText = _RM.GetString("textBox_Title.CueText");
                label_Error.Text = _RM.GetString("label_Error.Text");

            }
            catch (Exception e)
            {
                ErrorManager.WriteLog("PanelEvent", "UpdateUI", e, Param.RM.GetString("MB_Error_UpdateUI"));
            }
        }

        //New Event
        public void init(Guid ID)
        {
            _init = true;
            _ID = ID;

            initialState.d1 = DateTime.Today;
            initialState.d2 = DateTime.Today;
            initialState.periode = false;
            initialState.titre = string.Empty;
            initialState.lien = string.Empty;

            dateTimePicker_Date1.Value = initialState.d1;
            dateTimePicker_Date2.Value = initialState.d2;
            radioButton_date.Checked = !initialState.periode;
            textBox_Link.Text = initialState.lien;
            textBox_Title.Text = initialState.titre;

            newEvent = true;
            _init = false;
        }

        //Event edition
        public void init(string title, Uri link, DateTime d_start, DateTime d_end, bool period, Guid ID)
        {
            _init = true;
            _ID = ID;

            initialState.d1 = d_start;
            initialState.d2 = d_end;
            initialState.periode = period;
            initialState.titre = title;


            textBox_Title.Text = title;

            if (null != link && Uri.IsWellFormedUriString(link.AbsoluteUri, UriKind.Absolute))
            {
                initialState.lien = link.AbsoluteUri;
                textBox_Link.Text = link.AbsoluteUri;
            }
            else
            {
                initialState.lien = string.Empty;
                textBox_Link.Text = string.Empty;
            }

            dateTimePicker_Date1.Value = d_start;
            if (period)
            {
                dateTimePicker_Date2.Value = d_end;
                radioButton_period.Checked = true;
            }
            else
                radioButton_date.Checked = true;

            newEvent = false;
            _init = false;
        }

        public void reset()
        {
            dateTimePicker_Date1.Value = initialState.d1;

            if (initialState.periode)
            {
                dateTimePicker_Date2.Value = initialState.d2;
                radioButton_period.Checked = true;
            }
            else
                radioButton_date.Checked = true;

            textBox_Link.Text = initialState.lien;
            textBox_Title.Text = initialState.titre;
        }
    }
}
